% function [Sal, iSal, cSal, oSal, mSal, im2, pyrcurr] = saliency_hw_sim(fpath, nr, nc, Nc, frame_ref, pyrprev)
function [Sal, iSal, cSal, oSal, mSal, fSal] = saliency_hw_sim128(prev,curr, nr, nc, Nc, frame_ref)

% Parameters for orientation channel 
%% input parameters
% fpath = 'balloons.png'; %'pout.tif';
color_img = 1;  % flag to enable pre-processing if color-image

% nr =256;       %input image size - num of rows
% nc =256;       %input image size - num of cols

q = quantizer('fixed', [24,20]);    %input data representation

%% algorithmic parameters
dec_factor = 2; %decimate factor for each pyramid level
window = 5;     %filter window size in pyramid gen and resampling steps

% if(nr < 256)
%     Nc = 1;         %number of center channels
%     center_ch = [1];    %the actual center levels
%     delta = 6;      % number of levels of pyramid 
% else    
%     Nc = 3;         %number of center channels
%     center_ch = [1,2,3];    %the actual center levels
%     delta = 8;      % number of levels of pyramid 
% end

if(nr == 128)
    if(Nc == 1) %number of center channels
        center_ch = [0];    %the actual center levels
        delta = 5;      % number of levels of pyramid 
    elseif(Nc==2)
        center_ch = [0,1];    %the actual center levels
        delta = 6;      % number of levels of pyramid 
    elseif(Nc==3)    
        center_ch = [0,1,2];
        delta = 7;
    end    
else
    if(Nc == 1) %number of center channels
        center_ch = [1];    %the actual center levels
        delta = 6;      % number of levels of pyramid 
    elseif(Nc==2)
        center_ch = [1,2];    %the actual center levels
        delta = 7;      % number of levels of pyramid 
    elseif(Nc==3)    
        center_ch = [1,2,3];    %the actual center levels
        delta = 8;      % number of levels of pyramid 
    end
end

Ns = 2;         %num of surrounds for each center ch
del = [3, 4];   %delta for surround channel w.r.t each center ch

if(nr == 128)
    output_scale = 3;
else    
    output_scale = 4;   % the scale of the output saliency/conspicuity map
end

N_theta = 4;
theta = [0, pi/4, pi/2, 3*pi/4];
N_dir = 4;
dr = [0,1,1,1];
dc = [1,1,0,-1];



% outnorm = 0;    % flag to enable normalization after across-scale addition

if(window == 5)
    filt_coeff = (1/16)*[1, 4, 6, 4, 1];    %filter coefficients n each dimension
elseif(window == 9)
    filt_coeff = (1/64)*[1, 4, 8, 12, 14, 12, 8, 4, 1];
end

filt_coeff_2d = filt_coeff'*filt_coeff;     %filter coeffs for 2D convolution


%% pre-processing
if(color_img == 1)
    im1= prev;
%     im2 = imresize(im1, [nr, nc]);
%     img1 = double(im2)./255 ;
    img1 = double(prev)./255;
    
    im3=curr;
%     im4 = imresize(im3, [nr, nc]);
%     img2 = double(im4)./255 ;
    img2 = double(curr)./255;
    
    
    [I1 R1 G1 B1 Y1] = SalColorMap(img1);    
    [I2 R2 G2 B2 Y2] = SalColorMap(img2);    

    prev_img = I1; %abs(R - G);  
    curr_img = I2;
    
else
	 I1 = prev;
     I2 = curr;
    % I = imread(fpath);
    % Resize image to required matrix size
    % img1 = imresize(double(I), [nr,nc]);    
    % img = quantize(q, img1);    
end

%% Run the individual channels for conspicuity map computation

pars.img_size_x = nr;
pars.img_size_y = nc;
pars.levels = delta;
pars.Nc = Nc;
pars.Ns = Ns;
pars.c = center_ch; 
pars.delta = del;
pars.pyr_filt_coeff = filt_coeff_2d;
pars.resample_filt_coeff = filt_coeff_2d;
pars.outscale = output_scale;
pars.dec_factor = dec_factor;
pars.theta = theta(1);  %select a theta
pars.N_theta = N_theta;
pars.N_dir = N_dir;
pars.dr=dr(1);
pars.dc=dc(1);
pars.frame_ref=frame_ref;

 output_nr = nr/(dec_factor^output_scale);
 output_nc = nc/(dec_factor^output_scale);

I_q_curr = quantize(q,I2);
I_q_prev = quantize(q,I1);
RG_q_curr = quantize(q,R2-G2);
BY_q_curr = quantize(q,B2-Y2);
RG_q_prev = quantize(q,R1-G1);
BY_q_prev = quantize(q,B1-Y1);
% the intensity conspicuity map 
pars.pyr_select = 0;         %0 : gaussian pyramid, 1 : laplacian pyramid
pars.gabor_enable = 0;      %0 : gabor filter disable, 1 : gabor filter enable
pars.reichardt_enable=0;
pars.flicker_enable=0;
iSal = conspicuity_hw_sim(I_q_prev,I_q_curr, pars);

% the flicker conspicuity map 
pars.pyr_select = 0;         %0 : gaussian pyramid, 1 : laplacian pyramid
pars.gabor_enable = 0;      %0 : gabor filter disable, 1 : gabor filter enable
pars.reichardt_enable=0;
pars.flicker_enable=1;
if(frame_ref > 1)
    fSal = conspicuity_hw_sim(I_q_prev,I_q_curr, pars);
else
    fSal = zeros(output_nr,output_nc);
end

% the color conspicuity map 
pars.pyr_select = 0;         %0 : gaussian pyramid, 1 : laplacian pyramid
pars.gabor_enable = 0;      %0 : gabor filter disable, 1 : gabor filter enable
pars.reichardt_enable=0;
pars.flicker_enable=0;
RG_sim = conspicuity_hw_sim(RG_q_prev,RG_q_curr, pars);
BY_sim = conspicuity_hw_sim(BY_q_prev,BY_q_curr, pars);
cSal = maxnorm_sim(RG_sim + BY_sim);

% the orientation conspicuity map 
pars.pyr_select = 1;         %0 : gaussian pyramid, 1 : laplacian pyramid
pars.gabor_enable = 1;      %0 : gabor filter disable, 1 : gabor filter enable
pars.reichardt_enable=0;
pars.flicker_enable=0;
for k = 1:N_theta
    pars.theta = theta(k);
    O_theta_sim = conspicuity_hw_sim(I_q_prev,I_q_curr, pars);
    if(k == 1)
        orientation_ch_acc = O_theta_sim;
    else
        orientation_ch_acc = orientation_ch_acc + O_theta_sim;  % Accumulate the orientation map for each theta
    end
end
oSal = maxnorm_sim(orientation_ch_acc);      %Final normalization to get the consicuity map


pars.pyr_select = 0;         %0 : gaussian pyramid, 1 : laplacian pyramid
pars.gabor_enable = 0;      %0 : gabor filter disable, 1 : gabor filter enable
pars.reichardt_enable=1;
pars.flicker_enable=0;
if(frame_ref > 1)
    for k = 1:N_dir
        pars.dr = dr(k);
        pars.dc = dc(k);
    %    [motion_ch_sim, pyrcurr] = conspicuity_hw_sim(I_q, pars, pyrprev);
    %      I_q_prev
        [motion_ch_sim] = conspicuity_hw_sim(I_q_prev,I_q_curr, pars);
        if(k == 1)
            motion_ch_acc = motion_ch_sim;
        else
            motion_ch_acc = motion_ch_acc + motion_ch_sim;  % Accumulate the orientation map for each theta
        end
    end
    mSal = maxnorm_sim(motion_ch_acc);      %Final normalization to get the consicuity map
else
    mSal = zeros(output_nr,output_nc);
end
%save pars;

%% Combine the conspicuity maps to obtain the Saliency map
% Sal = (iSal + cSal + oSal)./3;
%% Combine the conspicuity maps to obtain the Saliency map
% Sal = (iSal + cSal + oSal )./3;   %ICO
% Sal = (iSal + oSal + fSal + mSal);    % FIOM
Sal = (mSal + oSal + iSal + cSal + fSal);   %CFIOM
% Sal = (oSal + iSal + cSal + fSal);   %CFIO
% Sal = mSal;
% Sal = (iSal + cSal + oSal)/3;

        
